var fs = require("fs");
var path = require('path');

var filesCount = 0;//debug目录下需要copy的文件数量
var copyed_filesCount = 0;//已经copy完成的文件数量
var srcFiles = "";

//1.读取index.html
function readIndex()
{
	let index_path = path.resolve(__dirname, '../GameHall/index.html');
	console.log(index_path);
	if(fs.existsSync(index_path))
	{
		let content = fs.readFileSync(index_path, 'utf-8');
		let start_key = "<!--game_files_start-->";
		let end_key = "<!--game_files_end-->";
		let start_index = content.indexOf(start_key) + start_key.length;
		let end_index = content.indexOf(end_key);
		console.warn("script:",start_index);
		
		let script_str = content.slice(start_index,end_index);
		script_str = script_str.trim();
		//console.log(script_str);
		
		//2.添加到mocha的index
		index_path = path.resolve(__dirname, './index.html');
		console.log(index_path);
		start_key = "<!--debug_files_start-->";
		end_key = "<!--debug_files_end-->";
		let content_0 = fs.readFileSync(index_path, 'utf-8');
		start_index = content_0.indexOf(start_key) + start_key.length;
		end_index = content_0.indexOf(end_key);
		let needReplace = content_0.slice(start_index,end_index);
		needReplace = needReplace.trim()
		//console.log(needReplace);
		content_0 = content_0.replace(needReplace,script_str);
		
		readSrc();
		start_key = "<!--src_files_start-->";
		end_key = "<!--src_files_end-->";
		start_index = content_0.indexOf(start_key) + start_key.length;
		end_index = content_0.indexOf(end_key);
		needReplace = content_0.slice(start_index,end_index);
		needReplace = needReplace.trim()
		console.log(needReplace);
		content_0 = content_0.replace(needReplace,srcFiles);
		
		writeFile(index_path,content_0);
	}
}

function writeFile(fileName,data)
{  
	fs.writeFile(fileName,data,'utf-8',complete);
	function complete(err)
	{
		console.log("文件生存完毕...",fileName);
		run();
	}
}

function copyDir(src, dist, callback,flag)
{
	//console.log(flag);
	fs.access(dist, function(err)
	{
		if(err)
		{
			//目录不存在时创建目录
			fs.mkdirSync(dist);
		}
		_copy(null, src, dist,flag);
	});
	
	function _copy(err, src, dist,flag) 
	{
		if(err)
		{
			callback(err);
		}
		else
		{
			var paths = fs.readdirSync(src);
			paths.forEach(function(_path)
			{
				var _src = src + '/' +_path;
				var _dist = dist + '/' +_path;
				//console.log(_path);
				
				fs.stat(_src, function(err, stat)
				{
					if(err)
					{
						callback(err);
					}
					else
					{
						// 判断是文件还是目录
						if(stat.isFile()) 
						{
							if(flag)
							{
								fs.writeFileSync(_dist, fs.readFileSync(_src));
								copyed_filesCount++;
								//console.log(filesCount,copyed_filesCount);
								if(filesCount == copyed_filesCount)//complete
								{
									console.log("bin-debug copy complete...");
									readIndex();
								}
							}
							else
							{
								filesCount++;
								//console.log(filesCount);
							}
						}
						else if(stat.isDirectory()) 
						{
							// 当是目录是，递归复制
							copyDir(_src, _dist, callback,flag);
						}
					}
				})
			})
		}
	}
}

function readSrc()
{
	_read(path.resolve(__dirname, './src'),'src');
	console.log(srcFiles);
	var len = srcFiles.length;
	srcFiles = srcFiles.slice(0,len-1);
}

function _read(src,root)//读取文件夹
{
	var paths = fs.readdirSync(src);
	var timeStrap = new Date().getTime(); 
	paths.forEach(function(_path)
	{
		var _src = src + '/' +_path;
		var _root = root + '/' + _path;
		var stats = fs.statSync(_src);
		
		if (stats.isDirectory())
		{
			_read(_src,_root);
		}
		else if(stats.isFile())
		{
			//<script src="bin-debug/AssetAdapter.js"></script>
			srcFiles += '<script src="' + _root + '?' + timeStrap + '"></script>\n';
		}
	})
}

function run()
{
	var c = require('child_process');
 
	c.exec("index.html");
}

function copyError(msg)
{
	console.warn(msg);
}

function start()
{
	console.log(filesCount);
	copyDir(path.resolve(__dirname, '../GameHall/bin-debug'),path.resolve(__dirname, './bin-debug'),copyError,true);
}

console.log("bin-debug copy start...");
copyDir(path.resolve(__dirname, '../GameHall/bin-debug'),path.resolve(__dirname, './bin-debug'),copyError,false);
setTimeout(start,500);
//copyDir(path.resolve(__dirname, '../GameHall/bin-debug'),path.resolve(__dirname, './bin-debug'),copyError,true);
